"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
import requests

from interface_L3.base.base_api import BaseApi
from interface_L3.utils.log_util import logger
from interface_L3.utils.utils import Utils


class Wework(BaseApi):

    '''
    完成企业微信接口 access_token 获取
    '''

    def __init__(self):
        # self.base_url = "https://qyapi.weixin.qq.com"
        # 调用读取 config 内容的方法
        self.get_config()
        self.token = self.get_access_token()

    def get_access_token(self):
        # print(self.base_url)
        # 定义凭证
        # corpid = "ww876064acebf0fa3c"
        # corpsecret = "A7LgEhs_Ty_dYXO9BcgY00eULrOghx4UyjqQoQNLc3o"
        url = f"{self.base_url}/cgi-bin/gettoken"
        # 定义param
        params = {
            "corpid": self.corpid,
            "corpsecret": self.corpsecret
        }
        # 发get请求
        # r = requests.request(method="GET", url=url, params=params)
        req = {
            "method": "GET",
            "url": url,
            "params": params
        }
        r = self.send_api(req)
        # print(r.text)
        # 提取响应中的 access_token 值
        token = r.json()["access_token"]
        return token

    def get_config(self):
        '''
        读取 yaml 配置文件中的数据
        '''
        file_path = f"{Utils.get_file_path()}/config/test_dev.yaml"
        logger.info(f"文件路径为 {file_path}")
        yaml_data = Utils.get_yaml_data(file_path)
        self.base_url = yaml_data.get("base_url")
        self.corpid = yaml_data.get("corpid").get("hogwarts")
        self.corpsecret = yaml_data.get("corpsecret").get("hogwarts").get("contact")

